<?php
class links_rss
{
	var $mydirname;
	var $controller_name;
	var $Links;

	/** Constructor
	 *
	**/
	function links_rss($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();

		if ($this->Links->is_mobile) {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 3, 'PC only');
		}
	}

	function index($cid = 0)
	{
		global $xoopsConfig;

		//channel
		$config_handler =& xoops_gethandler('config');
		if (defined('XOOPS_CUBE_LEGACY')) {
			$moduleHandler =& xoops_gethandler('module');
			$legacyRender =& $moduleHandler->getByDirname('legacyRender');
			$meta =& $config_handler->getConfigsByCat(0, $legacyRender->get('mid'));
		} else {
			$meta =& $config_handler->getConfigsByCat(XOOPS_CONF_METAFOOTER);
		}
		$channel_desc = htmlspecialchars($meta['meta_description'], ENT_QUOTES, _CHARSET);
		$channel_webmaster = htmlspecialchars($meta['meta_author'], ENT_QUOTES, _CHARSET);
		$channel_editor = str_replace('@', ' atmark ', $xoopsConfig['adminmail']);

		if ($cid > 0) $this->Links->setWhere('cid', "%[$cid]%", 'LIKE');
		$this->Links->setWhere('status', 0, '>');
		$this->Links->setWhere('date', time(), '<');
		$this->Links->setOrder('date DESC');
		$links = $this->Links->getDatas(20);// per_page:20

		$datas = array();
		foreach ($links as $link) {
			$link_url = $this->Links->makeUrl('index', 'item', array('lid' => $link['lid']));
			$author = empty($link['guestname']) ? $this->Links->getUnameFromUid($link['submitter']): $link['guestname'];

			$category = array();
			foreach ($this->Links->allkeycid as $cid) {
				if(in_array($cid, $link['cid'])) $category[] = xoops_utf8_encode($this->Links->allcid[$cid]);
			}
			$category = implode(', ', $category);

			$datas[] = array(
				'title'       => xoops_utf8_encode($link['title']),
				'link'        => $link_url,
				'description' => xoops_utf8_encode(htmlspecialchars($link['description'], ENT_QUOTES, _CHARSET)),
				'pubdate'     => date('r', $link['date']),
				'guid'        => $link['lid'],
				'author'      => xoops_utf8_encode($author),
				'category'    => $category
			);
		}

		if (function_exists('mb_http_output')) mb_http_output('pass') ;
		header ('Content-Type:text/xml; charset=utf-8');
		require_once XOOPS_ROOT_PATH . '/class/template.php' ;
		$tpl =& new XoopsTpl() ;
		$tpl->assign(array(
				'links'               => $datas,
				'channel_title'       => xoops_utf8_encode(htmlspecialchars($xoopsConfig['sitename'] ." - ". $this->Links->module_name, ENT_QUOTES, _CHARSET)),
				'channel_link'        => $this->Links->baseUrl(TRUE),
				'channel_lastbuild'   => date('r', time()),
				'channel_generator'   => 'links version ' . htmlspecialchars(_LINKS_VERSION, ENT_QUOTES, _CHARSET) . ' - http://never-ever.info/',
				'channel_description' => xoops_utf8_encode($channel_desc),
				'channel_category'    => 'XOOPS2|XCL Links module',
				'channel_editor'      => xoops_utf8_encode($channel_editor),
				'channel_webmaster'   => xoops_utf8_encode($channel_webmaster),
				'channel_language'    => _LANGCODE,
			)
		);
		$tpl->display('db:'. $this->mydirname . '_rss.html');
	}

	function category($cid = 0)
	{
		$this->index($cid);
	}

}